package com.chinatechstar.base.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.chinatechstar.admin.entity.SysUser;
import com.chinatechstar.admin.mapper.SysUserMapper;
import com.chinatechstar.base.entity.SysArea;
import com.chinatechstar.component.commons.exception.AjaxExceptionEnum;
import com.chinatechstar.component.commons.utils.CurrentUserUtils;
import com.chinatechstar.component.commons.utils.StrUtils;
import com.chinatechstar.component.commons.utils.ValidateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinatechstar.base.entity.SysProdOrg;
import com.chinatechstar.base.mapper.SysProdOrgMapper;
import com.chinatechstar.base.service.SysProdOrgService;
import com.chinatechstar.component.commons.result.PaginationBuilder;
import com.chinatechstar.component.commons.utils.SequenceGenerator;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 业务逻辑实现层
 */
@Service
@Transactional
public class SysProdOrgServiceImpl implements SysProdOrgService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static final SequenceGenerator SEQUENCE_GENERATOR = new SequenceGenerator();

    @Autowired
    private SysProdOrgMapper sysProdOrgMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询所有数据
     */
    @Override
    public List<SysProdOrg> findSysProdOrgAll() {
        return sysProdOrgMapper.findSysProdOrgAll();
    }

    /**
     * 查询分页
     */
    @Override
    public Map<String, Object> findSysProdOrgByCondition(Integer currentPage, Integer pageSize, String name, String sorter) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        if (StringUtils.isNotBlank(sorter)) {
            String sort = sorter.substring(0, sorter.lastIndexOf('_'));
            String sequence = "ascend".equals(sorter.substring(sorter.lastIndexOf('_') + 1)) ? "ASC" : "DESC";
            paramMap.put("sort", sort);
            paramMap.put("sequence", sequence);
        } else {
            paramMap.put("sort", "createTime");
            paramMap.put("sequence", "DESC");
        }
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<LinkedHashMap<String, Object>> resultList = sysProdOrgMapper.findSysProdOrgByCondition(paramMap);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), currentPage, pageSize);
    }

    /**
     * 查询导出数据列表
     */
    @Override
    public List<LinkedHashMap<String, Object>> findSysProdOrgForExcel(Map<String, Object> paramMap) {
        return sysProdOrgMapper.findSysProdOrgByCondition(paramMap);
    }

    /**
     * 根据主键ID数据查询数据
     */
    @Override
    public SysProdOrg findSysProdOrgById(Long id) {
        return sysProdOrgMapper.findSysProdOrgById(id);
    }

    /**
     * 根据主键ID数组查询数据
     */
    @Override
    public List<SysProdOrg> findSysProdOrgByIds(Long[] ids) {
        return sysProdOrgMapper.findSysProdOrgByIds(ids);
    }

    /**
     * 新增
     */
    @Override
    public void insertSysProdOrg(SysProdOrg sysProdOrg) {
        ValidateUtils.validateOrgId(sysProdOrg.getProdOrgId());
        ValidateUtils.validateOrgName(sysProdOrg.getProdOrgName());

        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        sysProdOrg.setId(SEQUENCE_GENERATOR.nextId());
        sysProdOrg.setCreateName(currUser.getNickname());
        sysProdOrg.setCreateUsername(currUser.getUsername());
        sysProdOrgMapper.insertSysProdOrg(sysProdOrg);
        logger.info("已新增： {}", sysProdOrg.getId());
    }

    /**
     * 批量新增
     */
    @Override
    public void insertSysProdOrgs(List<SysProdOrg> sysProdOrgs) {
        SysUser currUser = sysUserMapper.getUserByUsername(CurrentUserUtils.getOAuth2AuthenticationInfo().get("name"));

        for (SysProdOrg sysProdOrg : sysProdOrgs) {
            ValidateUtils.validateOrgId(sysProdOrg.getProdOrgId());
            ValidateUtils.validateOrgName(sysProdOrg.getProdOrgName());

            sysProdOrg.setId(SEQUENCE_GENERATOR.nextId());
            sysProdOrg.setCreateName(currUser.getNickname());
            sysProdOrg.setCreateUsername(currUser.getUsername());
        }
        sysProdOrgMapper.insertSysProdOrgs(sysProdOrgs);
    }

    /**
     * 编辑
     */
    @Override
    public void updateSysProdOrg(SysProdOrg sysProdOrg) {
        sysProdOrgMapper.updateSysProdOrg(sysProdOrg);
        logger.info("已编辑： {}", sysProdOrg.getId());
    }

    /**
     * 批量编辑
     */
    @Override
    public void updateSysProdOrgs(List<SysProdOrg> sysProdOrgs) {
        sysProdOrgMapper.updateSysProdOrgs(sysProdOrgs);
    }

    /**
     * 删除
     */
    @Override
    public void deleteSysProdOrgById(Long id) {
        sysProdOrgMapper.deleteSysProdOrgById(id);
    }

    /**
     * 批量删除
     */
    @Override
    public void deleteSysProdOrgByIds(Long[] ids) {
        sysProdOrgMapper.deleteSysProdOrgByIds(ids);
    }

    /**
     * 软删除
     */
    @Override
    public void softDeleteSysProdOrgById(Long id) {
        SysProdOrg sysProdOrg = sysProdOrgMapper.findSysProdOrgById(id);
        if (sysProdOrg != null) {
            sysProdOrg.setSoftDelete(0);
            sysProdOrgMapper.updateSysProdOrg(sysProdOrg);
        }

    }

    /**
     * 批量软删除
     */
    @Override
    public void softDeleteSysProdOrgByIds(Long[] ids) {
        List<SysProdOrg> sysProdOrgs = sysProdOrgMapper.findSysProdOrgByIds(ids);

        if (sysProdOrgs.size() > 0) {
            for (SysProdOrg sysProdOrg : sysProdOrgs) {
                sysProdOrg.setSoftDelete(0);
            }

            sysProdOrgMapper.updateSysProdOrgs(sysProdOrgs);
        }
    }

}
